<?php

namespace app\command;

use app\admin\model\Config;
use app\model\DistributionSetting;
use app\model\Feed;
use app\model\Talent;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use util\Logger;
use Wechat\CommerceApp;

class FeedTaskCommand extends Command
{
    protected function configure()
    {
        $this->setName('feed')
            ->setDescription('获取达人平台推广的短视频信息');
    }

    protected function execute(Input $input, Output $output)
    {
        Log::init([
            'type'        => 'file',
            'apart_level' => ['error'],
            'level'       => ['error'],
            'path'        => app()->getRootPath() . "runtime/log/feed/",
            'max_files'   => 5,
        ]);
        try {
            $mini_program_appid = Config::where('name', 'wx_xcx_appid')->value('value');
            $talent_list        = Talent::column('talent_appid');
            foreach ($talent_list as $talent_appid) {
                $next_key = '';
                do {
                    [$feed_list, $next_key, $has_more] = CommerceApp::getInstance()->getFeedList('', $talent_appid);
                    $next_key = $next_key ?? '';
                    $has_more = $has_more ?? false;

                    if ($feed_list) {
                        $ids = $save = [];
                        foreach ($feed_list as $item) {
                            $ids[] = $item['product_info']['product_id'];
                        }
                        $lists = Feed::where('talent_appid', $talent_appid)->where('product_id', 'in', $ids)->column('product_id, id');

                        foreach ($feed_list as $item) {
                            $promotion_info = CommerceApp::getInstance()->getFeedInfo($talent_appid, $mini_program_appid, $item['export_id']);
                            $product_info   = $item['product_info'];
                            if (!isset($lists[$product_info["product_id"]])) {
                                $save[] = [
                                    'talent_appid'        => $talent_appid,
                                    'export_id'           => $item['export_id'],
                                    'product_id'          => $product_info['product_id'],
                                    'product_name'        => $product_info['product_name'],
                                    'product_img_url'     => $product_info['product_img_url'],
                                    'product_price'       => $product_info['product_mini_price'],
                                    'commission'          => $item['predict_commission_amount'],
                                    'type'                => 1,
                                    'feed_token'          => $promotion_info[0]['feed_token'] ?? '',
                                    'promoter_share_link' => $promotion_info[0]['promoter_share_link'] ?? '',
                                ];
                            }
                        }
                        if ($save) {
                            (new Feed())->saveAll($save);
                        }
                    }
                } while ($has_more);
            }
        } catch (\Exception $e) {
            $log = [
                'msg'   => '获取达人列表失败',
                'error' => $e->getMessage(),
                'file'  => $e->getFile(),
                'line'  => $e->getLine(),
            ];
            Log::error($log);
            exit;
        }

    }
}
